<?php
/**
 * Class for work with database table "categories"
 * 
 * @version 2013-05-08
 * @package model
 * @subpackage categories
 * @category Db model
 */

class CategoriesModel extends Db_Table
{
	/**
     * __construct
     * This construct function connects to the table "categories".
     *
     * @version 2013-05-08
     * @uses parent::__construct('categories')
     * @return object
     */
	function __construct()
	{
		parent::__construct("categories");
	}

	/**
    * getProjectList
    * This function returns an array of records with categories.
    * 
    * @param int $id (account id)
    * @return array row
    * @access public
    */    
     public function getCategoryList($where=array(),$pager = 100000, $sort=array()) {
          $map = array(
               'id'           =>   'ct.id',
               'title'        =>   'ct.title',
               'status'       =>   'ct.status',
          );
     
          $sql = "SELECT COUNT(*) FROM {$this->table} AS ct " 
               .'WHERE '.$this->_parseWhere($where, $map);
        $cnt = $this->db->getField($sql);
          if (!$cnt)
            return array();
          if ($pager){
            if (is_object($pager))
                $limit = $pager->init($cnt);
            else 
                $limit = intVal($pager);
        }

          $sql = "SELECT "
            .$this->_joinFields($map)
            ." FROM {$this->table} as ct "
               ."WHERE ".$this->_parseWhere($where, $map)." "
            .$this->_parseOrder($sort)
            ." LIMIT ".$limit."";
          return $this->db->getRows($sql);
    }
	
	/**
    * getMemberCategories
    * This function returns an array of records with categories.
    * 
    * @param int $member_uuid (account id)
    * @return array row
    * @access public
    */ 
	
	public function getMemberCategories($member_uuid){
		$sql = "SELECT DISTINCT 
						cat.id AS id,
						cat.title AS title 
					FROM data_keys AS dk 
						LEFT JOIN categories AS cat ON (dk.category_id = cat.id) 
							WHERE dk.member_uuid = '".$member_uuid."'  AND cat.status = 'active' ORDER BY cat.title";
		$result = $this->db->getRows($sql);					
		array_unshift($result,array('id'=>0,'title'=>'Common'));
		
		return $result;
	}
	
	/**
    * deleted
	* This fucnction changes the status of an account to 'deleted'.
	*
	* @version 2013-05-08
	* @param integer $id
	* @param integer $blocked_id
	* @return boolean
	* @access public
	*/
	public function delete($id)
	{
		$cfg = array('status'=>'deleted');
		return $this->update($cfg,(int)$id);
	}
	
	/**
	* unblocked
    * This fucnction changes the status of an account to 'active'.
	*
	* @version 2013-05-08
	* @param integer $id
	* @return boolean
	* @access public
	*/
	public function activate($id)
	{
		$cfg = array('status'=>'active');
		return $this->update($cfg,(int)$id);
	}
	
    /**
	* inactive
	* This function sets 'inactive' status for a given category. 
	*
	* @version 2013-05-08
	* @param integer $id complaint ID
	* @param integer $inactive_id inactive account's ID
	* @return boolean
	* @access public
	*/
	public function inactive($id,$inactive_id)
	{
		$cfg = array('status'=>'inactive');
		return $this->update($cfg,(int)$id);
	}

    /**
	* getIdTitle
	* This function get 'title' category. 
	*
	* @version 2013-05-08
	* @param integer $id complaint ID
	* @param integer $inactive_id inactive account's ID
	* @return boolean
	* @access public
	*/
	public function getIdTitle($title,$insert = false)
	{
		$category = $this->findBy(array('title'=>'="'.$title.'"'));
		if($category[0]['id']){
			$id = $category[0]['id'];
			if($category[0]['status'] != 'active'){
				$this->update(array('status'=>'active'),$category[0]['id']);
			}
		}else if($insert and !empty($title)){
			$id = $this->insert(array('title'=>$title));
		}
		return (int)$id;
	}
}